package bs.app.android;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;

public class Implement {
	
	public static Implement instance = null;
	public Display  display;
	private MIDlet mid = null;
	private CanvasJ2ME can = null;
	private Graphics g = null;
	
    /**
     * Create a simple handler that we can use to cause animation to happen.  We
     * set ourselves as a target and we can use the sleep()
     * function to cause an update/invalidate to occur at a later date.
     */
    private RefreshHandler mRedrawHandler = new RefreshHandler();

    class RefreshHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
        	////System.out.println("Message.What="+msg.what);
        	update();
        }

        public void sleep(long delayMillis) {
        	this.removeMessages(0);
            sendMessageDelayed(obtainMessage(0), delayMillis);
        }
    };
    
    long tnow, tend, tdelay;
    long tupdate, tpost, twait; // debug
    final int MOVE_DELAY = 1000/25;
    int cnt=0;
    public void update() {
    	tnow = System.currentTimeMillis();
    	can.run();
    	
	tupdate=System.currentTimeMillis();
//	//System.out.println("Update:= "+(tupdate-tnow));
    	
    	Static.view.postInvalidate();
    	
	tpost=System.currentTimeMillis();
//	//System.out.println("Post:= "+(tpost-tupdate));
    }
    
	public Implement(Activity app, View view) {
		display=((WindowManager)app.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		instance = this;
		Static.App = app;
		Static.view = view;
	}
	public void setCanvas(CanvasJ2ME cv){
		can = cv;
	}
	public void _startApp(MIDlet midlet){
		mid = midlet; 
		this.start();
	}
	public void start() {
		try {
			mid.startApp();
			can.showNotify();
			mRedrawHandler.sleep(1);
		} catch (MIDletStateChangeException e) {
			e.printStackTrace();
		}
	}
	public void destroy() {
		try {
			mid.destroyApp(true);
		} catch (MIDletStateChangeException e) {
			e.printStackTrace();
		}
	}
	public void pause() {
		mid.pauseApp();
		can.hideNotify();
	}
	public void keyDown(int keyCode) {
		can.keyPressed(keyCode);
	}
	public void keyUp(int keyCode) {
		can.keyReleased(keyCode);
	}
	public void keyRepeated(int keyCode) {
		can.keyRepeated(keyCode);
	}
	public void pointerDragged(int x, int y) {
		can.pointerDragged(x, y);
	}
	public void pointerPressed(int x, int y) {
		can.pointerPressed(x, y);
	}
	public void pointerReleased(int x, int y) {
		can.pointerReleased(x, y);
	}
/* -------------------- Direct Screen Access */	
	public void resize(int width, int height){
		can.sizeChanged(width, height);
	}
	public void paint(Canvas canvas) {
	twait = System.currentTimeMillis();
//	//System.out.println("Wait:= "+(twait-tupdate)+" -> "+(twait-tpost));
		
		if(g == null) g = new Graphics(canvas);
		
		g.resetTranslate();
		can.paint(g);
		
		tend=System.currentTimeMillis();
//	//System.out.println("Draw:= "+(tend-twait));
		tdelay = MOVE_DELAY - (tend-tnow);
    	if (tdelay <= 0) 
    		mRedrawHandler.sleep(0);
		else
			mRedrawHandler.sleep(tdelay);
	}
	
///* -------------------- Buffer Screen Access */
//	private Image img = null;
//	private void createScreenBuffer(int w, int h){
//		img = Image.createImage(w, h);
//		g = img.getGraphics();
//	}
//	public void resize(int width, int height){
//		if(img != null && img.bmp != null) img.bmp.recycle();
//		createScreenBuffer(width, height);
//		can.sizeChanged(width, height);
//	}
//	public void paint(Canvas canvas) {
//	twait = System.currentTimeMillis();
//	//System.out.println("Wait:= "+(twait-tupdate)+" -> "+(twait-tpost));
//		
//		g.resetTranslate();
//		g.setClip(0, 0, can.getWidth(), can.getHeight());
//		can.paint(g);
//		
//		canvas.drawBitmap(img.bmp, 0, 0, null);
//		
//		tend=System.currentTimeMillis();
//	//System.out.println("Draw:= "+(tend-twait));
//		
//		tdelay = MOVE_DELAY - (tend-tnow);
//    	if (tdelay <= 0) 
//    		mRedrawHandler.sleep(0);
//		else
//			mRedrawHandler.sleep(tdelay);
//	}
///* ------------------------------------------*/
}
